package com.bjqg.bjqg01.bjqg.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.adapter.PreEntrustEntryClmcAdapter;
import com.bjqg.bjqg01.bjqg.bean.ClBean;
import com.bjqg.bjqg01.bjqg.bean.GcdmBean;
import com.bjqg.bjqg01.bjqg.utils.ToastUtil;
import com.bjqg.bjqg01.bjqg.view.WheelView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/4/11 0011.
 */

public class PreEntrustNumberEntryActivity extends BaseActivity implements View.OnClickListener {

    private ImageView goback;
    private TextView toptitle;
    private ImageView select;
    private RelativeLayout gcdm;
    private TextView wtdw;
    private TextView wtr;
    private TextView wtrdh;
    private RelativeLayout clmc;
    private Button btnsend;
    private ArrayList<String> catalogs;
    private ArrayList<String> catalogs2;
    private ArrayList<FenLei> calist;
    private TextView clmctv;
    private RecyclerView rvpre;
    private PreEntrustEntryClmcAdapter preentrustentryadapter;
    private ArrayList<ClBean> list;
    private TextView gcdmstr;
    private CheckBox checkgcfswt;
    private CheckBox checkqygrwt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pre_entrust_num_entry);
        initView();
    }

    private void initView() {
        goback = (ImageView) findViewById(R.id.back);
        toptitle = (TextView) findViewById(R.id.tv_top_title);
        select = (ImageView) findViewById(R.id.select);

        checkgcfswt = (CheckBox) findViewById(R.id.pr_entrust_num_entry_check_gcfswt);//工程复试委托
        checkqygrwt = (CheckBox) findViewById(R.id.pr_entrust_num_entry_check_qygrwt);//企业/个人委托
        gcdm = (RelativeLayout) findViewById(R.id.pr_entrust_num_entry_gcdm);//工程代码选择
        gcdmstr = (TextView) findViewById(R.id.pr_entrust_num_entry_gcdmstr);//工程代码
        wtdw = (TextView) findViewById(R.id.pr_entrust_num_entry_wtdw);//委托单位
        wtr = (TextView) findViewById(R.id.pr_entrust_num_entry_wtr);//委托人
        wtrdh = (TextView) findViewById(R.id.pr_entrust_num_entry_wtrdh);//委托人电话
        clmc = (RelativeLayout) findViewById(R.id.pr_entrust_num_entry_clmc);//材料名称选择
        clmctv = (TextView) findViewById(R.id.clmc_tv);//材料名称
        rvpre = (RecyclerView) findViewById(R.id.pr_entrust_num_entry_rv);//


        btnsend = (Button) findViewById(R.id.project_information_btn_send);//

        goback.setOnClickListener(this);
        toptitle.setText("预委托编号录入");
        select.setVisibility(View.INVISIBLE);

        gcdm.setOnClickListener(this);
        clmc.setOnClickListener(this);
        checkgcfswt.setOnClickListener(this);
        checkqygrwt.setOnClickListener(this);
        btnsend.setOnClickListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvpre.setLayoutManager(layoutManager);

        list = new ArrayList<>();
        preentrustentryadapter = new PreEntrustEntryClmcAdapter(R.layout.item_pre_entrust_entry, list);

        rvpre.setAdapter(preentrustentryadapter);

        getList();
        getfenleilist();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.pr_entrust_num_entry_gcdm:
                Intent intent = new Intent(PreEntrustNumberEntryActivity.this, ResultGcdmActivity.class);
                startActivityForResult(intent, 10);
                break;
            case R.id.pr_entrust_num_entry_clmc:
//                Intent intent = new Intent(this, ResultClmcActivity.class);
//                startActivityForResult(intent, 20);
                index = 0;
                indexright = 0;
                showDialogWheelView();
                break;
            case R.id.project_information_btn_send:

                break;
            case R.id.pr_entrust_num_entry_check_gcfswt:
                checkgcfswt.setChecked(true);
                checkqygrwt.setChecked(false);
                break;
            case R.id.pr_entrust_num_entry_check_qygrwt:
                checkgcfswt.setChecked(false);
                checkqygrwt.setChecked(true);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode== 20 && data != null){
            String resultclmc = data.getStringExtra("clmc");
            String resultdj = data.getStringExtra("dj");
            String resultjcxm = data.getStringExtra("jcxm");
            clmctv.setText(resultclmc);
            if (isclmc(resultclmc)){
                list.add(new ClBean(catalogs.get(index),catalogs2.get(indexright),resultclmc,resultdj,resultjcxm));
                preentrustentryadapter.setNewData(list);
            }else{
                ToastUtil.show(getApplicationContext(),"当前材料已选择");
            }
        }else if (requestCode == 10 && data!=null){
            GcdmBean resultgcdm = (GcdmBean) data.getParcelableExtra("gcdm");
            gcdmstr.setText(resultgcdm.getGcdm());
            wtdw.setText(resultgcdm.getWtdw());
            wtr.setText(resultgcdm.getWtr());
            wtrdh.setText(resultgcdm.getWtrdh());
        }
    }

    private boolean isclmc(String resultclmc) {
        for (int i = 0; i < list.size(); i++) {
            if (resultclmc.equals(list.get(i).getClmc())){
                return false;
            }
        }
        return true;
    }

    int index = 0;
    int indexright = 0;
    private void showDialogWheelView() {
        View outerView = LayoutInflater.from(this).inflate(R.layout.wheel_view_two, null);
        final WheelView wvle = (WheelView) outerView.findViewById(R.id.wheel_view_wv_left);
        final WheelView wvright = (WheelView) outerView.findViewById(R.id.wheel_view_wv_right);

        wvle.setOffset(2);
        wvright.setOffset(2);

//                wv.setItems(Arrays.asList(PLANETS));
        wvle.setItems(catalogs);
        wvright.setItems(catalogs2);
        wvle.setSeletion(0);
        wvright.setSeletion(0);
        wvle.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
//                Log.d("[Dialog]selectedIndex: " , selectedIndex + ", item: " + item);
                index = selectedIndex-2;
                int getfenlei = getfenlei(index);
                wvright.setSeletion(getfenlei);
                indexright = getfenlei;

            }
        });
        wvle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wvle.setSeletion(v.getId());
            }
        });
        wvright.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
//                Log.d("[Dialog]selectedIndex: " , selectedIndex + ", item: " + item);
                indexright = selectedIndex - 2;
//                getright(indexright);
                int num = calist.get(indexright).getNum();
                wvle.setSeletion(num);
                index = num;

            }
        });

        AlertDialog alertDialog = new AlertDialog.Builder(this)
//                .setTitle("WheelView in Dialog")
                .setView(outerView)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String strleft = catalogs.get(index);
                        String strright = catalogs2.get(indexright);

                        Intent intent = new Intent(PreEntrustNumberEntryActivity.this, ResultClmcActivity.class);
                        intent.putExtra("strleft",strleft);
                        intent.putExtra("strright",strright);
                        startActivityForResult(intent, 20);
//                        ToastUtil.show(getApplicationContext(),catalogs.get(index) +"/"+catalogs2.get(indexright));
                    }
                })
                .show();

        alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                Log.e("showDialogWheelView: ", wvle.getSeletedItem()+"/"+wvright.getSeletedItem());
            }
        });

    }

    private int getfenlei(int index) {
        for (int i = 0; i < calist.size(); i++) {
            if (index == calist.get(i).getNum()){
                return i;
            }
        }
        return 0;
    }

    public void getList(){
        catalogs = new ArrayList<String>();
        catalogs2 = new ArrayList<String>();
        String[] str = {"混凝土类","钢材类","材料类","配合比","防水卷材","砖，砌块类","保温材料类","复页报告委托"};
        String[] str2 = {"混凝土抗压","混凝土抗渗","混凝土抗折","混凝土抗冻","混凝土抗拌合用水",
                "砂浆抗压","钢筋原材","钢筋焊接","钢筋机械连接","高强度螺栓","钢绞线",
                "水泥","砂子","石子","土壤干密度","土壤击实","外加剂","预拌砂浆","压实度","陶瓷砖","粉煤灰","轻集料",
                "门窗","开关","插座","电线电缆","波纹管","塑料管材","塑料管件","建筑涂料","铝塑复合板","放射性",
                "腻子","石材","甲醛","建筑幕墙","通用材料","保温材料通用","电器材料通用","砼配合比","砂浆配合比","防水卷材","防水涂料",
                "轻集料混凝土小型空心砌块","普通混凝土小型空心砌块","烧结普通（多孔）砖","烧结空心砖","蒸压加气混凝土砌块",
                "其它砖","抗裂砂浆","绝热用模塑聚苯乙烯泡沫塑料","胶粉聚苯乙烯颗粒保温浆料","耐碱网布","界面砂浆","抹面胶浆",
                "墙体保温用膨胀聚苯乙烯板胶粘剂","胶粉料","锚栓抗拉承载力","绝热用挤聚苯乙烯泡沫塑料","岩棉","橡塑保温管",
                "胶粘剂","找平砂浆","混凝土界面处理剂","企业送检","抽检样品","试验分析报告","内部送样、评审"};
//        catalogs= (ArrayList<String>) Arrays.asList(str);
//        catalogs2= (ArrayList<String>) Arrays.asList(str2);
        for (int i = 0; i < str.length; i++) {
            catalogs.add(str[i]);
        }
        for (int i = 0; i < str2.length; i++) {
            catalogs2.add(str2[i]);
        }
    }

    class FenLei{
        private int Num;
        private String Lei;

        public int getNum() {
            return Num;
        }

        public void setNum(int num) {
            Num = num;
        }

        public String getLei() {
            return Lei;
        }

        public void setLei(String lei) {
            Lei = lei;
        }

        public FenLei(int num, String lei) {
            Num = num;
            Lei = lei;
        }
    }

    public void getfenleilist(){
        calist = new ArrayList<FenLei>();
        calist.add(new FenLei(0,"混凝土类"));
        calist.add(new FenLei(0,"混凝土抗渗"));
        calist.add(new FenLei(0,"混凝土抗折"));
        calist.add(new FenLei(0,"混凝土抗冻"));
        calist.add(new FenLei(0,"混凝土抗拌合用水"));
        calist.add(new FenLei(0,"砂浆抗压"));
        calist.add(new FenLei(1,"钢筋原材"));
        calist.add(new FenLei(1,"钢筋焊接"));
        calist.add(new FenLei(1,"钢筋机械连接"));
        calist.add(new FenLei(1,"高强度螺栓"));
        calist.add(new FenLei(1,"钢绞线"));
        calist.add(new FenLei(2,"水泥"));
        calist.add(new FenLei(2,"砂子"));
        calist.add(new FenLei(2,"石子"));
        calist.add(new FenLei(2,"土壤干密度"));
        calist.add(new FenLei(2,"土壤击实"));
        calist.add(new FenLei(2,"外加剂"));
        calist.add(new FenLei(2,"预拌砂浆"));
        calist.add(new FenLei(2,"压实度"));
        calist.add(new FenLei(2,"陶瓷砖"));
        calist.add(new FenLei(2,"粉煤灰"));
        calist.add(new FenLei(2,"轻集料"));
        calist.add(new FenLei(2,"门窗"));
        calist.add(new FenLei(2,"开关"));
        calist.add(new FenLei(2,"插座"));
        calist.add(new FenLei(2,"电线电缆"));
        calist.add(new FenLei(2,"波纹管"));
        calist.add(new FenLei(2,"塑料管材"));
        calist.add(new FenLei(2,"塑料管件"));
        calist.add(new FenLei(2,"建筑涂料"));
        calist.add(new FenLei(2,"铝塑复合板"));
        calist.add(new FenLei(2,"放射性"));
        calist.add(new FenLei(2,"腻子"));
        calist.add(new FenLei(2,"石材"));
        calist.add(new FenLei(2,"甲醛"));
        calist.add(new FenLei(2,"建筑幕墙"));
        calist.add(new FenLei(2,"通用材料"));
        calist.add(new FenLei(2,"保温材料通用"));
        calist.add(new FenLei(2,"电器材料通用"));
        calist.add(new FenLei(3,"砼配合比"));
        calist.add(new FenLei(3,"砂浆配合比"));
        calist.add(new FenLei(4,"防水卷材"));
        calist.add(new FenLei(4,"防水涂料"));
        calist.add(new FenLei(5,"轻集料混凝土小型空心砌块"));
        calist.add(new FenLei(5,"普通混凝土小型空心砌块"));
        calist.add(new FenLei(5,"烧结普通（多孔）砖"));
        calist.add(new FenLei(5,"烧结空心砖"));
        calist.add(new FenLei(5,"蒸压加气混凝土砌块"));
        calist.add(new FenLei(5,"其它砖"));
        calist.add(new FenLei(6,"抗裂砂浆"));
        calist.add(new FenLei(6,"绝热用模塑聚苯乙烯泡沫塑料"));
        calist.add(new FenLei(6,"胶粉聚苯乙烯颗粒保温浆料"));
        calist.add(new FenLei(6,"耐碱网布"));
        calist.add(new FenLei(6,"界面砂浆"));
        calist.add(new FenLei(6,"抹面胶浆"));
        calist.add(new FenLei(6,"墙体保温用膨胀聚苯乙烯板胶粘剂"));
        calist.add(new FenLei(6,"胶粉料"));
        calist.add(new FenLei(6,"锚栓抗拉承载力"));
        calist.add(new FenLei(6,"绝热用挤聚苯乙烯泡沫塑料"));
        calist.add(new FenLei(6,"岩棉"));
        calist.add(new FenLei(6,"橡塑保温管"));
        calist.add(new FenLei(6,"胶粘剂"));
        calist.add(new FenLei(6,"找平砂浆"));
        calist.add(new FenLei(6,"混凝土界面处理剂"));
        calist.add(new FenLei(7,"企业送检"));
        calist.add(new FenLei(7,"抽检样品"));
        calist.add(new FenLei(7,"试验分析报告"));
        calist.add(new FenLei(7,"内部送样、评审"));
    }
}
